#include "dialog_lightcontrol.h"
#include "ui_dialog_lightcontrol.h"
#include "QDebug"
Dialog_LightControl::Dialog_LightControl(QWidget *parent, comboBoxItem currItem) :
    QDialog(parent),
    m_currItem(currItem),
    ui(new Ui::Dialog_LightControl)
{
    ui->setupUi(this);


    // 显示1通道的数值范围
    QString channelMinVal = QString::number(ui->spinBox_A->minimum());
    QString channelMaxVal = QString::number(ui->spinBox_A->maximum());
//    ui->label_Channel_1_minVal->setText(channelMinVal);
//    ui->label_Channel_1_maxVal->setText(channelMaxVal);
    // 显示2通道的数值范围
    channelMinVal = QString::number(ui->spinBox_B->minimum());
    channelMaxVal = QString::number(ui->spinBox_B->maximum());
//    ui->label_Channel_2_minVal->setText(channelMinVal);
//    ui->label_Channel_2_maxVal->setText(channelMaxVal);


    // QSpinBox设置为当前滑动条值
    ui->spinBox_A->setValue(ui->hSlider_A->value());
    // 连接信号和槽
    connect(ui->hSlider_A, &QSlider::valueChanged, this, [=](int value) {
        ui->spinBox_A->setValue(value);
    });
    connect(ui->spinBox_A, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
             ui->hSlider_A, &QSlider::setValue);

    // 连接信号和槽
    connect(ui->hSlider_A, &QSlider::valueChanged, this, [=](int value) {
        ui->spinBox_A->setValue(value);
    });
    connect(ui->spinBox_A, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
             ui->hSlider_A, &QSlider::setValue);




    // QSpinBox设置为当前滑动条值
    ui->spinBox_B->setValue(ui->hSlider_B->value());
    // 连接信号和槽
    connect(ui->hSlider_B, &QSlider::valueChanged, this, [=](int value) {
        ui->spinBox_B->setValue(value);
    });
    connect(ui->spinBox_B, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
             ui->hSlider_B, &QSlider::setValue);

    // 连接信号和槽
    connect(ui->hSlider_B, &QSlider::valueChanged, this, [=](int value) {
        ui->spinBox_B->setValue(value);
    });
    connect(ui->spinBox_B, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
             ui->hSlider_B, &QSlider::setValue);

    ui->comboBox_lightName->setCurrentIndex(m_currItem);
//        ui->comboBox_lightName->currentIndexChanged(m_currItem);
    ui->comboBox_lightName->setEnabled(false);
    if(m_currItem == Fill)
    {
        ui->label_Channel_1->setText("");
        ui->label_Channel_2->setText("侧面补光光源(返回)");
//            ui->comboBox_lightName->currentIndexChanged(Bottom);
        ui->comboBox_lightName->setCurrentIndex(Bottom);
        ui->comboBox_lightName->setEnabled(false);
        ui->spinBox_A->setEnabled(false);
        ui->hSlider_A->setEnabled(false);
    } else if(m_currItem == Top){
        ui->label_Channel_1->setText("顶部相机1光源(返回)");
        ui->label_Channel_2->setText("顶部相机2光源");
    } else if(m_currItem == Side){
        ui->label_Channel_1->setText("侧面相机1光源(返回)");
        ui->label_Channel_2->setText("侧面相机2光源");
    } else if(m_currItem == Bottom){
        ui->label_Channel_1->setText("底部相机光源(返回)");
        ui->label_Channel_2->setText("");
        ui->spinBox_B->setEnabled(false);
        ui->hSlider_B->setEnabled(false);
    }
    else if(m_currItem == Null)
    {
//            ui->comboBox_lightName->currentIndexChanged(0);
        ui->comboBox_lightName->setCurrentIndex(0);
        ui->comboBox_lightName->setEnabled(true);
//        ui->spinBox_B->setEnabled(false);
//        ui->hSlider_B->setEnabled(false);
    }

//        ui->label_Channel_1->setText("顶部相机1光源");
//        ui->label_Channel_2->setText("顶部相机2光源");

    connect(ui->comboBox_lightName, &QComboBox::currentTextChanged, [=](QString Item){
        if (Item == "底部"){
            ui->label_Channel_1->setText("底部相机光源");
            ui->label_Channel_2->setText("侧面补光光源");
        } else if(Item == "顶部") {
            ui->label_Channel_1->setText("顶部相机1光源");
            ui->label_Channel_2->setText("顶部相机2光源");
        } else if(Item == "侧面") {
            ui->label_Channel_1->setText("侧面相机1光源");
            ui->label_Channel_2->setText("侧面相机2光源");
        }

        on_btn_readA_clicked();
        on_btn_readB_clicked();
    });

}

Dialog_LightControl::~Dialog_LightControl()
{
    delete ui;
}

QString Dialog_LightControl::getSetLightsDataString(const QString &channel, const uint8_t channelVal)
{
    QString sData="";

    QString lightChannel = "S";

    lightChannel += channel;

    uint8_t val = channelVal;
    if(val < 100) lightChannel += "0";
    if(val < 10) lightChannel += "0";
    sData += lightChannel + QString("0%1#").arg(val);

    return sData;
}

void Dialog_LightControl::getRecv(QString data)
{
    qDebug() << "光源->返回(ASCII)：" << data;

    if(data.size() > 2)
    {  
        QString channelName = "a";
        channelName += "b";
        if(ui->comboBox_lightName->currentText() == "侧面")
        {
            if(channelName.size() >= 2)
            {
                channelName[0] = 'c';
                channelName[1] = 'd';
            }
        }

        for(int i = 0; i < channelName.size(); ++i)
        {
            int aPos = data.indexOf(channelName.at(i));
            if(aPos != -1)
            {
                char Channel = data.at(aPos).toLatin1();
                QString lightValueStr = data.mid(aPos+1, 4).leftJustified(4,'0');

                qDebug() << "lightValueStr:" << lightValueStr;

                uint8_t lightValue = lightValueStr.toUInt();

//                if(ui->comboBox_lightName->currentText() == "底部")
//                {
//                    ui->spinBox_A->setValue(lightValue);
//                    return;
//                }

                // 读取亮度值
                switch(Channel)
                {
                    case 'a':{
                        ui->spinBox_A->setValue(lightValue);
                    }break;
                    case 'b':{
                        ui->spinBox_B->setValue(lightValue);
                    }break;
                    case 'c':{
                        ui->spinBox_A->setValue(lightValue);
                    }break;
                    case 'd':{
                        ui->spinBox_B->setValue(lightValue);
                    }break;
                }
            }
        }
    }
}

void Dialog_LightControl::on_btn_sendData_clicked()
{
    QString lightChannel_1 = "S";
    QString lightChannel_2 = "S";

    if(ui->comboBox_lightName->currentText() == "侧面")
    {
        lightChannel_1 += "C";
        lightChannel_2 += "D";
    } else {
        lightChannel_1 += "A";
        lightChannel_2 += "B";
    }

    QString sData="";
    if(ui->hSlider_A->isEnabled())
    {
        uint8_t val = ui->spinBox_A->value();
        if(val < 100) lightChannel_1 += "0";
        if(val < 10) lightChannel_1 += "0";
        sData += lightChannel_1 + QString("0%1#").arg(val);
    }

    if(ui->hSlider_B->isEnabled())
    {
        uint8_t val = ui->spinBox_B->value();
        if(val < 100) lightChannel_2 += "0";
        if(val < 10) lightChannel_2 += "0";
        sData += lightChannel_2 + QString("0%1#").arg(val);
    }

    qDebug()<<"emit:"<<sData;

    if(ui->comboBox_lightName->currentText() == "底部")
    {
        emit sendBottomLightAdjustData(sData);
    } else {
        emit sendTopRoSideLightAdjustData(sData);
    }
}

void Dialog_LightControl::on_btn_readA_clicked()
{

    QString sData="SA#";

    if(ui->comboBox_lightName->currentText() == "侧面")
    {
        // 侧面光源1
        sData = "SC#";
    }

    qDebug()<<"emit:"<<sData;
    if(ui->comboBox_lightName->currentText() == "底部")
    {
//        // 读取侧面补光
//        sData += "SB#";

        // 读取底部光源
        emit sendBottomLightAdjustData(sData);
    } else {
        // 读取顶部
        emit sendTopRoSideLightAdjustData(sData);
    }
}

void Dialog_LightControl::on_btn_readB_clicked()
{
    QString sData="SB#";

    if(ui->comboBox_lightName->currentText() == "侧面")
    {
        sData = "SD#";
    }
    qDebug()<<"emit:"<<sData;

    if(ui->comboBox_lightName->currentText() == "底部")
    {
        // 侧面补光
        emit sendBottomLightAdjustData(sData);
    } else {
        // 顶部光源和侧面光源
        emit sendTopRoSideLightAdjustData(sData);
    }
}

void Dialog_LightControl::do_btn_sendState_clicked()
{
    QString sData="";
    if(ui->hSlider_A->isEnabled())
    {
        sData = "SH#";
    } else if(ui->hSlider_A->isEnabled())
    {
        sData = "SL#";
    }
    qDebug()<<"emit:"<<sData;
    emit sendTopRoSideLightAdjustData(sData);
}

void Dialog_LightControl::on_btn_ok_clicked()
{
    if(m_currItem == Fill)
    {
        emit returnData(ui->hSlider_B->value());
    } else {
        emit returnData(ui->hSlider_A->value());
    }

    accept();
}

void Dialog_LightControl::on_btn_exit_clicked()
{
    reject();
}

void Dialog_LightControl::on_comboBox_lightName_activated(const QString &arg1)
{

}
